﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RailwayReservationWeb.manager
{
    public partial class fare_rules : System.Web.UI.Page
    {
        private RailwayReservationDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new RailwayReservationDataContext();
            pMessage.Visible = false;
        }

        private void FillPopupData()
        {
            var index = txtTrainTypeId.Value;
            if (!String.IsNullOrWhiteSpace(index))
            {
                int id;
                if (!Int32.TryParse(index, out id))
                {
                    Response.Redirect("~/manager/fare_rules.aspx");
                }
                var ob = db.Fares.First(p => p.TrainTypeId.Equals(id));
                if (ob != null)
                {
                    txtTrainTypeId.Value = ob.TrainTypeId.ToString();
                    txtTrainType.Text = ob.TrainType.TrainTypeName;
                    txt1AC.Text = ob._1AC.ToString();
                    txt2AC.Text = ob._2AC.ToString();
                    txt3AC.Text = ob._3AC.ToString();
                    txtSleeper.Text = ob.Sleeper.ToString();
                    txtGeneral.Text = ob.General.ToString();
                }
            }
        }

        protected string GetTrainTypeName(int trainTypeId)
        {
            var trainType = db.TrainTypes.First(p => p.TrainTypeId == trainTypeId);
            if (trainType != null)
            {
                return trainType.TrainTypeName;
            }
            return trainTypeId.ToString();
        }

        protected void gvwFares_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "AltEdit")
            {
                txtTrainTypeId.Value = e.CommandArgument.ToString();
                FillPopupData();
                phModal.Controls.Clear();
                phModal.Controls.Add(new Literal()
                {
                    Text = "<script type=\"text/javascript\">" +
                           "$( document ).ready(function() {" +
                           "$('#myModal').modal(); });" +
                           "</script>"
                });
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (Page.IsValid)
                {
                    // ViewStateMode=ViewStateMode.Disabled;
                    var ob = db.Fares.First(p => p.TrainTypeId.Equals(Int32.Parse(txtTrainTypeId.Value.Trim())));
                    ob._1AC = Double.Parse(txt1AC.Text.Trim());
                    ob._2AC = Double.Parse(txt2AC.Text.Trim());
                    ob._3AC = Double.Parse(txt3AC.Text.Trim());
                    ob.Sleeper = Double.Parse(txtSleeper.Text.Trim());
                    ob.General = Double.Parse(txtGeneral.Text.Trim());
                    db.SubmitChanges();
                    gvwFares.DataBind();
                    Response.Redirect("~/manager/fare_rules.aspx");
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        private void ShowMessage(string msg)
        {
            pMessage.Visible = true;
            pMessage.InnerText = msg;
        }

    }
}